package com.shop.dao;

import com.shop.bean.Product;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDAO extends BaseDAO {

    // 关键修改：获取未售出商品（包括上架1、冻结2，排除售出3）
    public Product getActiveProduct() {
        try (Connection conn = getConnection();
             PreparedStatement ps = conn.prepareStatement("SELECT * FROM product WHERE status IN (1,2) LIMIT 1")) {
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                Product p = new Product();
                p.setId(rs.getInt("id"));
                p.setName(rs.getString("name"));
                p.setDescription(rs.getString("description"));
                p.setPrice(rs.getDouble("price"));
                p.setImagePath(rs.getString("image_path"));
                p.setStatus(rs.getInt("status"));
                p.setCreatedTime(rs.getTimestamp("created_time"));
                return p;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 发布新商品（默认上架状态1）
    public void addProduct(Product product) {
        try (Connection conn = getConnection();
             PreparedStatement ps = conn.prepareStatement(
                     "INSERT INTO product(name,description,price,image_path,status) VALUES(?,?,?,?,1)")) {
            ps.setString(1, product.getName());
            ps.setString(2, product.getDescription());
            ps.setDouble(3, product.getPrice());
            ps.setString(4, product.getImagePath());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 更新商品状态（1=上架/解冻，2=冻结，3=售出）
    public void updateProductStatus(int productId, int status) {
        try (Connection conn = getConnection();
             PreparedStatement ps = conn.prepareStatement("UPDATE product SET status=? WHERE id=?")) {
            ps.setInt(1, status);
            ps.setInt(2, productId);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 获取所有历史商品（含售出）
    public List<Product> getAllProducts() {
        List<Product> list = new ArrayList<>();
        try (Connection conn = getConnection();
             PreparedStatement ps = conn.prepareStatement("SELECT * FROM product ORDER BY created_time DESC")) {
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Product p = new Product();
                p.setId(rs.getInt("id"));
                p.setName(rs.getString("name"));
                p.setDescription(rs.getString("description"));
                p.setPrice(rs.getDouble("price"));
                p.setImagePath(rs.getString("image_path"));
                p.setStatus(rs.getInt("status"));
                p.setCreatedTime(rs.getTimestamp("created_time"));
                list.add(p);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}